<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-09-02 10:17
 *
 * 项目：rm  -  $  - PayController.php
 *
 * 作者：liwei 
 */

namespace modules\levpays\modules\alipay\controllers;

use Lev;
use lev\base\Controllerv;
use lev\widgets\login\loginWidget;
use modules\levpays\levpaysHelper;
use modules\levpays\modules\alipay\sdk\aliminiPay;
use modules\levpays\modules\alipay\sdk\Payment;
use modules\levpays\table\tradesHelper;

!defined('INLEV') && exit('Access Denied LEV');


class PayController extends Controllerv
{

    public static function actionWap() {
        $tradeNo = Lev::stripTags(Lev::GPv('tradeNo'));
        $tradeInfo = tradesHelper::tradeInfo(tradesHelper::tradeNoToId($tradeNo));
        echo '正在调起支付....';
        Payment::wapPay(tradesHelper::getTradeno($tradeInfo['id']), $tradeInfo['name'], $tradeInfo['paymoney'], $tradeInfo['descs']);
    }

    public static function actionPc() {
        $tradeNo = Lev::stripTags(Lev::GPv('tradeNo'));
        $tradeInfo = tradesHelper::tradeInfo(tradesHelper::tradeNoToId($tradeNo));
        echo '正在调起支付....';
        Payment::pcPay(tradesHelper::getTradeno($tradeInfo['id']), $tradeInfo['name'], $tradeInfo['paymoney'], $tradeInfo['descs']);
    }

    public static function actionMini() {
        $tradeNo = Lev::stripTags(Lev::GPv('tradeNo'));
        $tradeInfo = tradesHelper::tradeInfo(tradesHelper::tradeNoToId($tradeNo));
        $buyer_id = Lev::stripTags(Lev::GPv('buyer_id'));
        $res = aliminiPay::createTrade($buyer_id, tradesHelper::getTradeno($tradeInfo['id']), $tradeInfo['name'], $tradeInfo['paymoney'], $tradeInfo['descs']);
        echo json_encode($res);
    }

    public static function actionReturnUrl() {
        Payment::ReturnUrl();
    }

    public static function actionNotify() {
        Payment::payNotify();
    }

    public static function actionCheckTradeNo() {
        $tradeNo = Lev::stripTags(Lev::GPv('tradeNo'));
        $pre = Lev::stripTags(Lev::GPv('pre'));
        //$res = Payment::checkTradeNo($tradeNo);//levpaysHelper::getOutTradeNo($tradeNo, $pre)
        $res = aliminiPay::queryTrade($tradeNo);
        if ($res['_paySuccess']) {
            Payment::paySuccess(['message'=>'已支付'], $tradeNo, $pre, $res);
            $location = loginWidget::getLoginReferer() ?: 1;
            $msg = (Lev::responseMsg(1, '支付成功', ['location'=>$location]));
        }elseif ($res['trade_status'] == 'WAIT_BUYER_PAY' || $res['code'] == 40004) {//未支付
            $msg = (Lev::responseMsg(2, '',[Lev::$app['isAdmin']? $res :'']));
        }else {
            if ($res['trade_status'] == 'TRADE_CLOSED') {//支付订单已关闭
                $tradeId = tradesHelper::tradeNoToId($tradeNo);
                tradesHelper::update(['tradestatus' => 2], ['id'=>$tradeId]);
                $msg = Lev::responseMsg(-302, '支付订单已关闭，请刷新');
            }else {
                $msg = Lev::responseMsg(-2, $res['trade_status'].''.$res['sub_msg'], [Lev::$app['isAdmin']? $res :'']);
            }
        }
        echo json_encode($msg);
    }
}